package com.exchange.code.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exchange.context.UserInfoContext;
import com.exchange.context.entity.UserInfo;
import com.exchange.code.dao.SysCodeTypeDao;
import com.exchange.code.dto.AddCodeTypeDto;
import com.exchange.code.dto.SelCodeTypeDto;
import com.exchange.code.entity.SysCodeType;
import com.exchange.code.service.SysCodeService;
import com.exchange.utils.result.CommonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;

/***
 *                    .::::.
 *                  .::::::::.
 *                 :::::::::::
 *             ..:::::::::::'
 *           '::::::::::::'
 *             .::::::::::
 *        '::::::::::::::..
 *             ..::::::::::::.
 *           ``::::::::::::::::
 *            ::::``:::::::::'        .:::.
 *           ::::'   ':::::'       .::::::::.
 *         .::::'      ::::     .:::::::'::::.
 *        .:::'       :::::  .:::::::::' ':::::.
 *       .::'        :::::.:::::::::'      ':::::.
 *      .::'         ::::::::::::::'         ``::::.
 *  ...:::           ::::::::::::'              ``::.
 * ```` ':.          ':::::::::'                  ::::..
 *                    '.:::::'                    ':'````..
 *
 * @Author liuhai
 * @Date 2020-11-24 16:29:44
 * @Description 代码管理接口实现类
 */
@Component
@RefreshScope
@Service
public class SysCodeServiceImpl implements SysCodeService {

    /**
     * 获取查询条件标识—新增
     */
    private final String GET_QUERY_ADD = "1";
    /**
     * 获取查询条件标识—查询
     */
    private final String GET_QUERY_SEL = "2";

    @Value("${params.codeType.parent_def}")
    private String codeTypeParentDef;
    @Value("${params.codeType.version_def}")
    private String codeTypeVersionDef;
    @Resource
    private SysCodeTypeDao sysCodeTypeDao;


    @Override
    public CommonResult addCodeType(AddCodeTypeDto codeTypeDto) {
        if(null == codeTypeDto){
            return CommonResult.fail("新增对象不能为空");
        }
        String typeParent = StringUtils.isEmpty(codeTypeDto.getCodeTypeParent()) ? codeTypeParentDef : codeTypeDto.getCodeTypeParent();
        String codeVersion = StringUtils.isEmpty(codeTypeDto.getCodeVersion()) ? codeTypeVersionDef : codeTypeDto.getCodeVersion();

        SelCodeTypeDto selCodeTypeDto = new SelCodeTypeDto();
        selCodeTypeDto.setCodeTypeName(codeTypeDto.getCodeTypeName());
        selCodeTypeDto.setCodeTypeParent(typeParent);
        selCodeTypeDto.setCodeVersion(codeVersion);
        int existCount = this.selectCount(selCodeTypeDto, "1");

        if(existCount > 0){
            return CommonResult.fail("代码分类名称已存在");
        }
        // 获取新增的代码分类编码值
        String newCode = sysCodeTypeDao.getNewTypeCode();
        SysCodeType newCodeType = new SysCodeType();
        BeanUtils.copyProperties(codeTypeDto, newCodeType);

        newCodeType.setCodeTypeCode(newCode);
        newCodeType.setCodeTypeParent(typeParent);
        newCodeType.setCodeVersion(codeVersion);

        UserInfo userInfo = UserInfoContext.getUser();
        if(null != userInfo){
            newCodeType.setCreateBy(userInfo.getUserId());
        }else {
            newCodeType.setCreateBy(0L);
        }

        newCodeType.setCreateTime(new Date());
        int addCount = sysCodeTypeDao.insert(newCodeType);
        if(addCount > 0){
            return CommonResult.successWithData("新增成功",newCodeType);
        }
        return CommonResult.fail("新增失败");
    }

    @Override
    public int selectCount(SelCodeTypeDto selCodeTypeDto, String flag) {
        return sysCodeTypeDao.selectCount(this.getQueryWrapper(selCodeTypeDto, flag));
    }

    @Override
    public IPage<SysCodeType> getCodeTypeList(SelCodeTypeDto selCodeTypeDto) {
        long current = selCodeTypeDto.getCurrent() == 0 ? 1 : selCodeTypeDto.getCurrent();
        long pageSize = selCodeTypeDto.getPageSize() == 0 ? 10 : selCodeTypeDto.getPageSize();
        return sysCodeTypeDao.selectMapsPage(new Page<>(current, pageSize), this.getQueryWrapper(selCodeTypeDto, "2"));
    }

    /**
     * 获取查询条件
     * @param selCodeTypeDto
     * @param flag 1：新增校验查询，2：查询数据
     * @return
     */
    public QueryWrapper getQueryWrapper(SelCodeTypeDto selCodeTypeDto, String flag){
        QueryWrapper codeTypeWrapper = new QueryWrapper<SysCodeType>();
        codeTypeWrapper.eq("code_type_status", selCodeTypeDto.getCodeTypeStatus());
        if(!StringUtils.isEmpty(selCodeTypeDto.getCodeTypeCode())){
            codeTypeWrapper.eq("code_type_code", selCodeTypeDto.getCodeTypeCode());
        }
        if(!StringUtils.isEmpty(selCodeTypeDto.getCodeTypeName())){
            if(GET_QUERY_ADD.equals(flag)){
                codeTypeWrapper.eq("code_type_name", selCodeTypeDto.getCodeTypeName());
            }else {
                codeTypeWrapper.like("code_type_name", selCodeTypeDto.getCodeTypeName());
            }
        }
        if(!StringUtils.isEmpty(selCodeTypeDto.getCodeTypeParent())){
            codeTypeWrapper.eq("code_type_parent", selCodeTypeDto.getCodeTypeParent());
        }
        if(!StringUtils.isEmpty(selCodeTypeDto.getCodeVersion())){
            codeTypeWrapper.eq("code_version", selCodeTypeDto.getCodeVersion());
        }
        return codeTypeWrapper;
    }
}
